package com.hkemmm.shop.controller;

import com.hkemmm.shop.entity.Car;
import com.hkemmm.shop.entity.Category;
import com.hkemmm.shop.entity.Goods;
import com.hkemmm.shop.entity.Goods_img;
import com.hkemmm.shop.service.ICarService;
import com.hkemmm.shop.service.ICategoryService;
import com.hkemmm.shop.service.ICommentService;
import com.hkemmm.shop.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private ICarService carService;
    @Autowired
    private ICommentService commentService;


    private String proUrl = "/myimg/";

    @ResponseBody
    @RequestMapping("/RandomGoods")
    public List<Goods> getGoods(Integer num) {
        List<Goods> list = goodsService.getGoods(num);

        for (Goods g : list) {
            if (g.getImg() != null) {
                g.setImg(proUrl + g.getImg());
            }
        }


        return list;
    }

    @ResponseBody
    @RequestMapping("/GetGoods")
    public HashMap getGoodsInfo(Integer id) {
        HashMap map = new HashMap();
        Goods goods = goodsService.selectByPrimaryKey(id);
        List<Goods_img> imgList = goodsService.getListImg(id);

        goods.setImg(proUrl + goods.getImg());

        for (Goods_img g : imgList) {
            if (g.getUrl() != null) {
                g.setUrl(proUrl + g.getUrl());
            }
        }

        map.put("goods", goods);
        map.put("imgList", imgList);

        return map;
    }

    @RequestMapping("/product")
    public ModelAndView product(Integer id, ModelAndView mav) {
        Goods goods = goodsService.selectByPrimaryKey(id);
        List<Goods_img> list = goodsService.getListImg(id);
        goods.setCategoryList(categoryService.getGoodsCategory(goods.getId()));

        goods.setImg(proUrl + goods.getImg());

        for (Goods_img g : list) {
            if (g.getUrl() != null) {
                g.setUrl(proUrl + g.getUrl());
            }
        }


        List<Goods> randGoods = goodsService.getGoods(6);


        List<Goods> related = new ArrayList<>();

        if (goods.getCategoryList().size() > 0) {
            related = goodsService.getRelatedGoods(goods.getCategoryList());
        }

        mav.addObject("goods", goods);
        mav.addObject("img", list);
        mav.addObject("related", related);
        mav.addObject("randGoods", randGoods);
        mav.addObject("commentNum", commentService.countCommentNum(id));

        mav.setViewName("/page/product");

        return mav;
    }


    @ResponseBody
    @RequestMapping("/addCar")
    public String addCart(Car car) {

        Car temp = carService.existGoods(car);
        if (temp == null) {
            car.setStatus(0);
            carService.insertSelective(car);
            return "ok";
        } else {
            temp.setNumber(car.getNumber() + temp.getNumber());
            carService.updateByPrimaryKeySelective(temp);

            return "ok";
        }


    }

    @ResponseBody
    @RequestMapping("/test")
    public List<Category> test() {
        return categoryService.getGoodsCategory(1);
    }
}
